package com.beiing.tianshuai.tianshuai.message.ui;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.adapter.MyGroupsAdapter;
import com.beiing.tianshuai.tianshuai.base.BaseActivity;
import com.beiing.tianshuai.tianshuai.entity.MyGroupsBean;
import com.beiing.tianshuai.tianshuai.entity.UserInfoBean;
import com.beiing.tianshuai.tianshuai.message.presenter.MyGroupsPresenter;
import com.beiing.tianshuai.tianshuai.message.view.MyGroupsViewImpl;
import com.beiing.tianshuai.tianshuai.nim.session.SessionHelper;
import com.beiing.tianshuai.tianshuai.util.DensityUtils;
import com.beiing.tianshuai.tianshuai.util.LogUtils;
import com.beiing.tianshuai.tianshuai.widget.CustomProgressDialog;
import com.beiing.tianshuai.tianshuai.widget.RecycleViewDivider;

public class MyGroupsActivity extends BaseActivity implements MyGroupsViewImpl{

    ImageView mToolbarIvBack;
    TextView mToolbarTvTitle;
    RecyclerView mLvMyGroups;

    private static final String     TAG = "MyGroupsActivity";
    private Context                 mContext;
    private MyGroupsAdapter         mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_groups;
    }

    @Override
    protected void init() {
        mContext = this;
        initView();
        initToolbar();
        initRecyclerView();
        initListener();
    }

    private void initView() {
        mToolbarIvBack = findViewById(R.id.toolbar_iv_back);
        mToolbarTvTitle = findViewById(R.id.toolbar_tv_title);
        mLvMyGroups = findViewById(R.id.lv_my_groups);
    }

    private void initToolbar() {
        mToolbarTvTitle.setText("我的群组");
        mToolbarIvBack.setVisibility(View.VISIBLE);
    }

    private void initRecyclerView() {
        mAdapter = new MyGroupsAdapter(mContext);
        mLvMyGroups.setAdapter(mAdapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        mLvMyGroups.setNestedScrollingEnabled(false);
        mLvMyGroups.setLayoutManager(linearLayoutManager);
        //绘制item间的分割线，
        mLvMyGroups.addItemDecoration(
                new RecycleViewDivider(
                        mContext, LinearLayoutManager.HORIZONTAL, DensityUtils.dp2px(mContext, 0.5f),
                        ContextCompat.getColor(mContext, R.color.common_gray_background))
        );
    }

    private void initListener() {
        //返回
        mToolbarIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        //列表点击事件
        mAdapter.setListener(new MyGroupsAdapter.OnItemClick() {
            @Override
            public void onItemClickListener(int position, MyGroupsBean.DataBean.NumberBean.AddBean bean) {
                SessionHelper.startTeamSession(mContext, bean.getGroupid());
            }
        });
    }

    private void initPresenter() {
        MyGroupsPresenter presenter = new MyGroupsPresenter(this);
        presenter.getMyGroupsRequestResponse(UserInfoBean.getUid(mContext));
    }

    @Override
    protected void onResume() {
        super.onResume();
        initPresenter();
    }

    @Override
    public void showProgress() {
        CustomProgressDialog.showLoading(mContext, "正在努力加载，请稍后...", true);
    }

    @Override
    public void hideProgress() {
        CustomProgressDialog.stopLoading();
    }

    @Override
    public void onRequestSuccess(MyGroupsBean bean) {
        if (bean.getCode() == 200) {
            mAdapter.updateData(bean.getData().getNumber().getAdd());
        } else {
            showShortText("哎呀不好了，加载出错了。");
        }
    }

    @Override
    public void onRequestFailed(Throwable throwable) {
        LogUtils.e(throwable.toString(), TAG);
        showShortText("网络异常");
    }
}
